package coupon_imp

import (
	
	"bp/conf"
	"bp/db"
	"bp/internal/service/com"
	"bp/internal/service/entity"
	"bp/internal/util"
	couponGrpc "bp/rpc/gen/backendapi/bcoupon"
	messageGrpc "bp/rpc/gen/shared/message"
	"context"
	"fmt"
	"github.com/golang/protobuf/ptypes/empty"
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

const (
	couponServiceName string = "bp.backendapi.CouponOpService"
)

var (
	allowPagerMethods              []string
	allowBilinTokenMethods         []string
	// allowAuthorizationTokenMethods []string
)

type CouponBackendApiImp struct {
	dao       *db.PgDao
	slog      *zap.SugaredLogger
	vars      *conf.VarsSection
	entityHub *entity.EntityHub
	comHub    *com.ComHub
	// cacher *cache.Adapter
	couponGrpc.CouponOpServiceServer
}

// NewOffTrialMWebApi initialization
func New(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	vars *conf.VarsSection,
	s *grpc.Server,
	entityHub *entity.EntityHub,
	comHub *com.ComHub,
	// adp *cache.Adapter,
) (cbs *CouponBackendApiImp) {
	cbs = &CouponBackendApiImp{
		dao:       pgdao,
		slog:      slog.With("module", "CouponBackendApi"),
		vars:      vars,
		entityHub: entityHub,
		comHub:    comHub,
		// cacher:           adp,
	}
	RegisterCouponToBilinToken()
	RegisterCouponToPager()

	couponGrpc.RegisterCouponOpServiceServer(s, cbs)
	return
}

// AllowCouponToBilinToken 注册允许访问BilinTokenMW的URL
func RegisterCouponToBilinToken() {
	allowBilinTokenMethods = append(allowBilinTokenMethods,
		fmt.Sprintf("/%s/GetCoupon", couponServiceName),
		fmt.Sprintf("/%s/ListCoupons", couponServiceName),
		fmt.Sprintf("/%s/ListCouponWithStats", couponServiceName),
	)
}

// AllowCouponToPager 注册允许访问PagerMW的URL
func RegisterCouponToPager() {
	allowPagerMethods = append(allowPagerMethods,
		fmt.Sprintf("/%s/ListCoupons", couponServiceName),
		fmt.Sprintf("/%s/ListCouponWithStats", couponServiceName),
	)
}

func (s *CouponBackendApiImp) AllowBilinTokenMW(method string) bool {
	return util.StringContains(allowBilinTokenMethods, method)
}

func (s *CouponBackendApiImp) AllowPagerMW(method string) bool {
	return util.StringContains(allowPagerMethods, method)
}

func (s *CouponBackendApiImp) PingCoupon(ctx context.Context, _ *empty.Empty) (res *messageGrpc.PingRes, err error) {
	res = &messageGrpc.PingRes{
		Msg: "ok",
	}
	return
}

func (s *CouponBackendApiImp) GetCoupon(ctx context.Context, req *couponGrpc.GetCouponReq) (res *couponGrpc.GetCouponRes, err error) {
	// token := s.getCtxToken(ctx)
	// s.slog.Infof("token=>%+v", token)

	res = &couponGrpc.GetCouponRes{}
	return
}

func (s *CouponBackendApiImp) ListCoupons(ctx context.Context, req *couponGrpc.ListCouponsReq) (res *couponGrpc.ListCouponsRes, err error) {
	// pager := s.getCtxPager(ctx)
	// s.slog.Infof("pager=>%+v", pager)

	res = &couponGrpc.ListCouponsRes{}
	return
}

func (s *CouponBackendApiImp) ListCouponWithStats(ctx context.Context, req *couponGrpc.ListCouponWithStatsReq) (res *couponGrpc.ListCouponWithStatsRes, err error) {
	// token := s.getCtxToken(ctx)
	// s.slog.Infof("token=>%+v", token)
	// pager := s.getCtxPager(ctx)
	// s.slog.Infof("pager=>%+v", pager)
	// s.slog.Infof("req=>%+v", req)
	res = &couponGrpc.ListCouponWithStatsRes{}
	return
}
